/** @file    wCore_SystemError.cpp
 *  @author  Alessandro Polo
 *  @version $Id: wCore_SystemError.cpp 2891 2010-08-12 01:58:59Z alex $
 *  @brief
 * File containing methods for the wosh::gui::wCore_SystemError class.
 * The header for this class can be found in wCore_SystemError.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "wCore_SystemError.h"

 #include <core/ObjectAllocator.h>
 #include <framework/gui/GuiUtilities.h>

 #include "ui_wCore_SystemError.h"


using namespace std;
namespace wosh {
 namespace gui {

 WOSH_REGISTER(wosh::gui::wCore_SystemError, "wosh::gui::IWidgetSerializable", 1.0, _static_wCore_SystemError )
 WOSH_GUI_MAP(wosh::gui::wCore_SystemError, "wosh::SystemError", 0.0, _static_Map_wCore_SystemError )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

wCore_SystemError::wCore_SystemError( QWidget* parent )
	: QWidget(parent) {

	this->m_ui = new Ui::wCore_SystemError();
	this->m_ui->setupUi(this);

	setWindowIcon( GuiUtilities::createIconFor( getSerializableType() ) );
	setWindowTitle( getSerializableType() );

	loadObject(NULL);
 }

wCore_SystemError::~wCore_SystemError() {
	delete this->m_ui;
 }

void wCore_SystemError::changeEvent(QEvent *e) {
	QWidget::changeEvent(e);
	if ( e->type() == QEvent::LanguageChange )
		this->m_ui->retranslateUi(this);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT wCore_SystemError::loadObject( const SystemError* notif ) {
	if ( notif == NULL ) {
		setWindowTitle("SystemError [NULL]");
		this->m_ui->txtID->setText("");
		this->m_ui->dteTimeStamp->clear();
		this->m_ui->cmbPriority->setCurrentIndex(0);
		this->m_ui->wSourceURI->setTargetURI( URI() );
		this->m_ui->txtMethod->setText("");
		this->m_ui->txtErrorCode->setText("");
		this->m_ui->txtDescription->setPlainText("");
	 }
	else {
		setWindowTitle( QString("SystemError#%1").arg(notif->getID()) );
		this->m_ui->txtID->setText( QString::number(notif->getID()) );
		QDateTime dt; dt.setTime_t( notif->getTimeStamp() );
		this->m_ui->dteTimeStamp->setDateTime(dt);
		this->m_ui->cmbPriority->setCurrentIndex( (int)notif->getPriority() );
		this->m_ui->wSourceURI->setTargetURI( notif->getSource() );
		this->m_ui->txtMethod->setText( notif->getMethod().c_str() );
		this->m_ui->txtErrorCode->setText( QString::number(notif->getErrorCode()) );
		this->m_ui->txtDescription->setPlainText( notif->getDescription().c_str() );
	 }
	return WRET_OK;
 }

WRESULT wCore_SystemError::saveObject( SystemError* notif ) const {
	if ( notif == NULL ) return WRET_ERR_PARAM;
/*
		this->m_ui->cmbPriority->setCurrentIndex(0);
		this->m_ui->txtPreferredCommunicator->text().toLatin1().data();
		this->m_ui->txtSenderUser->text().toLatin1().data();
		this->m_ui->txtSenderID->text().toLatin1().data();
		this->m_ui->txtRecipentUser->text().toLatin1().data();
		this->m_ui->txtRecipentID->text().toLatin1().data();
		this->m_ui->txtRecipentGroup->text().toLatin1().data();
		this->m_ui->chkReadTS->setChecked(false);
		this->m_ui->chkSentTS->setChecked(false);
		this->m_ui->txtReadTS->text().toLong();
		this->m_ui->txtSentTS->text().toLong()
*/
	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

QVariant SystemErrorTypeModel::data( const QModelIndex &index, int role ) const {
	if ( !index.isValid() )	return QVariant();
	if ( role == Qt::DisplayRole || role == Qt::EditRole ) { // cos completer!
		SystemError::ERROR_PRIORITY dPriority = SystemError::UNKNOWN;
		switch(index.row()) {
			case 0: dPriority = SystemError::UNKNOWN; break;
			case 1: dPriority = SystemError::INFO; break;
			case 2: dPriority = SystemError::WARNING; break;
			case 3: dPriority = SystemError::CRITICAL; break;
			default: break;
		 }
		return QVariant( SystemError::getPriorityAsString(dPriority) );
	}
	return QVariant();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////


 }; // namespace gui
}; // namespace wosh
